<?php
namespace app\wxapp\controller;
use app\common\entity\BackResult;
use app\common\entity\Result;
use app\common\service\WordService;
use app\common\xunfei\XFTrans;
use app\Request;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/20 0020
 * Time: 17:48
 */
class Word extends Base{
    public function trans(Request $request){
        $word = $request->param('word');
        if(empty($word)){
            return Result::error("没有单词");
        }
        $a = new XFTrans();
        $result = $a->xfyun($word);

        return Result::success($result);
    }


    /**
     * 用户加入到生词表 同时也将词 加入到词典表 如果词典表中不存在的话
     * @param Request $request
     * @return \think\Response
     */
    public function addWord(Request $request) {
        $word = $request->param("word");
        $videoId = $request->param('videoId');
        if(empty($word) || empty($videoId)){
            return BackResult::error("缺少参数");
        }
        $service = new WordService();
        try{
            $service->addWordToFavor($word,$this->user['id'],$videoId);
        }catch (\Exception $e){
            return BackResult::error($e->getMessage());
        }

        return BackResult::success('ok');
    }


    /**
     * 获取当前视频收藏的单词列表
     * @param Request $request
     * @return \think\Response
     */
    public function words(Request $request){
        $videoId = $request->param('videoId');
        $page = $request->param("page",1);
        $pageSize = $request->param("pageSize",20);
        $service = new WordService();
        $result = $service->wordsByVideo($videoId,$this->user['id'],$page,$pageSize);
        return Result::success($result);
    }

}